package com.ccys.cms.service.productAndService.impl;

import cn.hutool.core.util.IdUtil;
import com.ccys.cms.service.productAndService.ProductIngredientService;
import com.ccys.cms.service.productAndService.ProductService;
import com.ccys.common.vo.ResponseResult;
import com.ccys.db.cache.UserUtil;
import com.ccys.db.dto.web.productAndService.BatchIngredientDto;
import com.ccys.db.dto.web.productAndService.NumEnum;
import com.ccys.db.dto.web.productAndService.ProductDto;
import com.ccys.db.entity.TblAccount;
import com.ccys.db.entity.TblInventoryFeed;
import com.ccys.db.entity.TblProduct;
import com.ccys.db.entity.TblProductIngredient;
import com.ccys.db.mapper.TblProductMapper;
import com.ccys.db.page.PageDto;
import com.ccys.db.rely.service.impl.BaseServiceImpl;
import com.ccys.db.vo.web.inventory.ProductInfoVo;
import com.ccys.db.vo.web.productAndService.ProductVo;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

@Service
@Slf4j
public class ProductServiceImpl extends BaseServiceImpl<TblProduct> implements ProductService {


    @Resource
    private TblProductMapper mapper;

    @Resource
    private ProductIngredientService ingredientService;

    @Override
    public ResponseResult getPage(ProductDto dto) {

        PageDto.startPage();
        //根据当前用户groupid
        //超级管理员的区分 (超级管理员没有groupId 不会进查询条件)
        if (StringUtils.isEmpty(dto.getGroupId())) {
            String groupId = dto.getGroupId();
            dto.setGroupId(groupId);
        }
        List<ProductVo> list = mapper.getList(dto);
        PageInfo<ProductVo> info = new PageInfo<>(list);
        return ResponseResult.success(info);
    }

    @Override
    public void updateBatchImportQuantity(List<TblProduct> productList) {
        mapper.updateBatchImportQuantity(productList);
    }

    @Override
    public void increaseImportQuantity(Integer quantity, String id) {
        mapper.increaseImportQuantity(quantity, id);
    }

    @Override
    public void decreaseImportQuantity(Integer quantity, String id) {
        mapper.decreaseImportQuantity(quantity, id);
    }

    @Override
    public ProductInfoVo getProductInfo(String productId) {

        return mapper.getProductInfo(productId);
    }

    @Override
    public void updateStockQuantityByAdjust(TblInventoryFeed model) {
        mapper.updateStockQuantityByAdjust(model);
    }

    @Override
    public void deleteIngredients(String productId) {
        mapper.deleteIngredients(productId);
    }

    @Override
    public ResponseResult change(BatchIngredientDto model) {

        List<TblProductIngredient> ingredientList = model.getIngredientList();

        if (StringUtils.isEmpty(model.getId())) {
            //  非group层级账号添加 product 时
            // approve status 为待审核，需要 group 层级的管理员进行审核。
            // 同时 clinic 账号登录时可查看当前所属 group 全部 product，但是不能编辑
            // if(StringUitl.isnotblank(companyId||clinicId||pharmacyId)){
            //   需要审核，其中clinic和pharmacy添加的产品存储 clinicId
            TblAccount curLoginAccount = UserUtil.getCurLoginAccount();
            if (Objects.nonNull(curLoginAccount)) {
                String companyId = curLoginAccount.getCompanyId();
                String clinicId = curLoginAccount.getClinicId();
                String pharmacyId = curLoginAccount.getPharmacyId();
                if (StringUtils.isNotEmpty(companyId) || StringUtils.isNotEmpty(clinicId) || StringUtils.isNotEmpty(pharmacyId)) {
                    // 非group层级账号添加 product 时 approve status 为待审核
                    model.setApproveStatus(NumEnum.INACT.getCode());
                    if (StringUtils.isNotEmpty(clinicId) || StringUtils.isNotEmpty(pharmacyId)) {
                        model.setClinicId(clinicId);
                    }
                }
            }

        }
        //如果是修改同时Ingredient不为空
        //应删除之前对应的IngredientList
        this.deleteIngredients(model.getId());
        int i = this.insertOrUpdate(model);
        //批量插入ingredient
        if(CollectionUtils.isNotEmpty(ingredientList)){
            String productId = model.getId();
            ingredientList.forEach(x -> {
                x.setProductId(productId);
                x.setId(IdUtil.getSnowflakeNextIdStr());
            });
            ingredientService.insertAll(ingredientList);
        }
        return ResponseResult.update(i);
    }


}
